<?php
    
    class Article_model extends CI_Model{
        public function __construct()
        {
            $this->load->database();
            $this->load->library('session');
        }
        
        
        public function getAll()
        {
            $sql = "select a.*,c.title as cname from article as a, catalog as c where a.catalogid=c.id order by update_time desc";
            $query = $this->db->query($sql);
         
            return $query->result_array();
        }

        // public function getIndexData()
        // {
        //       $sql = "select a.*,c.title as cname from article as a, catalog as c where a.catalogid=c.id and a.catalogid=".$catalogid." order by update_time desc limit 10";
        //       $query = $this->db->query($sql);
         
        //     return $query->result_array();
        // }


    
        public function get($catalogid,$count = 10)
        {
            $sql = "select a.*,c.title as cname from article as a, catalog as c where a.catalogid=c.id and a.catalogid=".$catalogid." order by update_time desc limit ".$count;
            $query = $this->db->query($sql);
         
            return $query->result_array();
        }
       
        public function getindexdata($catalogid)
        {
            $sql = "select a.id,a.title,a.abstract,a.image from article as a where a.catalogid=".$catalogid." order by update_time desc limit 3";
            $query = $this->db->query($sql);
         
            return $query->result_array();
        }

        public function getOne($id)
        {
              $sql = "select * from article where id=".$id;
              $query = $this->db->query($sql);
              $res  = $query->row();

            return $res;
        }

        public function setEnable($id, $enable){
         $data = array(
                'enable' => $enable
                );

           $this->db->where('id', $id);  
           $this->db->update('article', $data);
        }

        public function add()
        {
            $data = array('title' => $this->input->post('title'),
                'catalogid' => $this->input->post('catalogid'),
                'update_time' => time(),
                'abstract' => $this->input->post('abstract'),
                'content' => $this->input->post('content'),
                'image' => $this->input->post('image')
                );
           $this->db->insert('article', $data);
        }
        
        public function update($id)
        {
            $data = array('title' => $this->input->post('title'),
                'catalogid' => $this->input->post('catalogid'),
                'update_time' => time(),
                'abstract' => $this->input->post('abstract'),
                'content' => $this->input->post('content'),
                'image' => $this->input->post('image')
                );

       
            
            $this->db->where('id', $id);  
            $this->db->update('article', $data);
        }
        
        public function getconfig()
        {
            $sql = "select * from config where id=1";
            $query = $this->db->query($sql);
            $res  = $query->row();

            return $res;
        }

        public function configsettings()
        {
            $data = array('website' => $this->input->post('website'),
                'keyword' => $this->input->post('keyword'),
                'descr' => $this->input->post('descr'),
                'beian' => $this->input->post('beian'),
                'homeimage' => $this->input->post('homeimage'),
                'contact' => $this->input->post('contact'),
                'friendlink' => $this->input->post('friendlink'),
                'copyright' => $this->input->post('copyright')
                );

            print_r($data);

            
            $this->db->where('id', 1);  
            $this->db->update('config', $data);
        }

        
        public function delete($id)
        {
            $this->db->where('id', $id);  
            $this->db->delete('article');
        }
        

}